<?php
namespace Dry\Base;

class Register {

    private $data = array();

    private $enable = array();

    private $disable = array();

    private function src($src = 'src')
    {
        $this->data['src'] = $src;
        return $this;
    }

    public function vendor($vendor)
    {
        $this->data['vendor'] = $vendor;
        return $this;
    }

    public function dir($dir)
    {
        $this->data['dir'] = $dir;
        return $this;
    }

    public function controller($controller = 'controller')
    {
        $this->data['controller'] = $controller;
        return $this;
    }

    public function enable($enable = '1')
    {
        $this->data['enable'] = $enable;
        return $this;
    }

    private function check()
    {
        if(!isset($this->data['src'])){
            $this->src();
        }
        if(!isset($this->data['controller'])){
            $this->controller();
        }
        if(!isset($this->data['enable'])){
            $this->enable();
        }
    }

    private function set()
    {
        $path = array($this->data['src'], $this->data['vendor'], $this->data['dir']);
        $path = implode('/', $path);
        $this->data['path'] = $path;
        $this->data['controllerPath'] = $path.'/'.$this->data['controller'];
        $this->data['namespace'] = $this->data['vendor'].'\\'.$this->data['dir'].'\\'.$this->data['controller'];
    }

    public function ok()
    {
        $this->check();
        $this->set();
        if($this->data['enable']=='0'){
            $this->disable[] = $this->data;
        }
        else{
            $this->enable[] = $this->data;
        }
        $this->data = array();
        return $this;
    }

    public function getEnable()
    {
        return $this->enable;
    }

    public function getDisable()
    {
        return $this->disable;
    }

    public function find($route)
    {
        if(empty($route)){
            exit;
        }
        $result = array();
        $list = $this->getEnable();
        foreach($list as $app){
            if($app['vendor']==$route['vendor'] && $app['dir']==$route['dir']){
                $result = $app;
                break;
            }
        }
        return $result;
    }

}
?>
